/**
 * 
 */
package fpt.pits.dao;

import java.sql.SQLException;

import fpt.pits.dto.DepartmentInfo;
import fpt.pits.util.Connection;

/**
 * @author yoyosakura
 *
 */
public class DepartmentInfoDAO {
    public DepartmentInfo get(int departID) {
        DepartmentInfo departmentInfo = null;
        java.sql.Connection cn = null;
        java.sql.PreparedStatement ps = null;
        java.sql.ResultSet rs = null;
        String query = null;
        try {
            query = "SELECT d.* " +
            		"FROM `department` d " +
            		"WHERE d.DepartID = ?";
            cn = Connection.getConnection();
            ps = cn.prepareStatement(query);
            ps.setInt(1, departID);
            rs = ps.executeQuery();
            while (rs.next()) {
                if (rs.getByte("IsDeleted") == 0) {
                    departmentInfo = new DepartmentInfo();
                    departmentInfo.setCompanyID(rs.getInt("CompanyID"));
                    departmentInfo.setDepartID(rs.getInt("DepartID"));
                    departmentInfo.setDepartName(rs.getString("DepartName"));
                    departmentInfo.setDescription(rs.getString("Description"));
                    departmentInfo.setIsDeleted(rs.getByte("IsDeleted"));
                    departmentInfo.setParentID(rs.getInt("ParentID"));
                } else {
                    return null;
                }
            }
        } catch (Exception e) {
            System.err.println("Error: Get data from database " + e.getMessage());
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (cn != null) {
                    cn.close();
                }
            } catch (SQLException sqle) {
                System.out.println("Error: No close connection " + sqle.getMessage());
            }
        }
        return departmentInfo;
    }
}
